package com.crm.system.controller.csd.customerManager;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.qo.exchangeProductsRecord.ExchangeProductsRecordQO;
import com.crm.model.qo.exchangeProductsRecord.ProductExchangeFormQO;
import com.crm.model.qo.exchangeProductsRecord.ProductExchangeaApprovalQO;
import com.crm.model.qo.finishOrderProductManager.FinishOrderProductQO;
import com.crm.model.vo.csd.product.ChangeCourseRecordVO;
import com.crm.model.vo.customerContractManager.CustomerChangeCourseRecordVO;
import com.crm.model.vo.exchangeProductsRecord.ExchangeProductsRecordVO;
import com.crm.model.vo.exchangeProductsRecord.ProductExchangeFormVO;
import com.crm.model.vo.exchangeProductsRecord.ProductExchangeaApprovalVO;
import com.crm.model.vo.finishOrderProduct.FinishOrderProductVO;
import com.crm.model.vo.productManager.CustomerMemberProductVO;
import com.crm.model.vo.productManager.CustomerProductVO;
import com.crm.system.service.csd.customerManager.ProductDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangpeng
 * @Date 2021/9/1 13:46
 */
@RestController
@RequestMapping("/csd/productManager")
public class ProductDetailsController {

    @Autowired
    private ProductDetailsService productDetailsService;

    /**
     * selectFinishOrderProductList
     *  产品管理 --产品明细
     * @Author wangpeng
     * @Date 2021/9/1
     * @param request	
     * @param pageIndex	
     * @param pageSize	
     * @param sortName	
     * @param sortOrder	
     * @param qo	
     * @return
     */
    @PostMapping("/selectProductDetailsList")
    private DataResponse selectProductDetailsList(HttpServletRequest request,
                                                      @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                      @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                      @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                      @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc
                                                      FinishOrderProductQO qo){

       try {
           DhPageInfo<FinishOrderProductVO> list = productDetailsService.selectProductDetailsList(pageIndex,pageSize,sortName,sortOrder,qo);
           return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
       }catch (Exception e){
           return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());

       }
    }

    /**
     * selectCustomerMemberProductList
     * 产品管理--产品明细--会员高级筛选
     * @Author wangpeng
     * @Date 2021/9/2
     * @param productStatus	//产品停/启用状态--0:启用;1:停用
     * @return
     */
    @PostMapping("/selectCustomerMemberProductList")
    private DataResponse selectCustomerMemberProductList( @RequestParam(value = "productStatus", required = true)Integer productStatus){
        try {
            List<CustomerMemberProductVO> list = productDetailsService.selectCustomerMemberProductList(productStatus);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);

        }catch (Exception e){
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * selectCustomerProductList
     * 产品管理--产品明细--非会员高级筛选
     * @Author wangpeng
     * @Date 2021/9/2
     * @param shelfStatus	上架状态：0-待定；1-已上架； 2-已下架
     * @return
     */
    @PostMapping("/selectCustomerProductList")
    private DataResponse selectCustomerProductList( @RequestParam(value = "shelfStatus", required = true)Integer shelfStatus){
        try {
            List<CustomerProductVO> list = productDetailsService.selectCustomerProductList(shelfStatus);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);

        }catch (Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * selectChangeCourseCecordList
     * 兑换审批列表
     * @Author wangpeng
     * @Date 2021/10/26
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param vo
     * @return
     */
    @RequestMapping("/selectChangeCourseCecordList")
    public DataResponse selectChangeCourseCecordList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                     @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                     @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                     @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                     ChangeCourseRecordVO vo
    ) {
        try{
            DhPageInfo<CustomerChangeCourseRecordVO> list = productDetailsService.selectChangeCourseCecordList(pageIndex,pageSize,sortName,sortOrder,vo);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
        }catch(Exception e){
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }

    }


    /**
     * selectExamineAndApproveList
     * 兑换审批 --审批
     * @Author wangpeng
     * @Date 2021/10/29
     * @param changeCourseId
     * @return
     */
    @RequestMapping("/selectExamineAndApproveList")
    public DataResponse selectExamineAndApproveList(String changeCourseId) {
        try {
            List<CustomerChangeCourseRecordVO> list = productDetailsService.selectExamineAndApproveList(changeCourseId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
        }catch (Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }

    }


    /**
     * exchangeApproval
     * 兑换审批 --审批提交
     * @Author wangpeng
     * @Date 2021/10/22
     * @param changeCourseId	兑换课程记录id
     * @param examine	审核状态 0:通过 1:待审批 2：不通过
     * @param remarks   备注
     * @param courseId  高端班传多个id
     * @return
     */
    @RequestMapping("/exchangeApproval")
    public DataResponse exchangeApproval(String changeCourseId,String examine,String remarks,String[] courseId,String[] changeCourseTitle,String ruleTitle) {
        try {
            Map map = productDetailsService.exchangeApproval(changeCourseId,examine,remarks,courseId,changeCourseTitle,ruleTitle);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),map);
        }catch (Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }

    }


    /**
     * selectExchangeCenterList
     * 兑换中心列表
     * @Author wangpeng
     * @Date 2021/10/26
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param vo
     * @return
     */
    @RequestMapping("/selectExchangeCenterList")
    public DataResponse selectExchangeCenterList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                     @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                     @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                     @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                     ChangeCourseRecordVO vo
    ) {
        try{
            DhPageInfo<CustomerChangeCourseRecordVO> list = productDetailsService.selectExchangeCenterList(pageIndex,pageSize,sortName,sortOrder,vo);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
        }catch(Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }

    }


    /**
     * selectExchangeDetails
     * 客户管理 -- 兑换中心 -- 产品明细
     * @Author wangpeng
     * @Date 2021/11/11
     * @param changeCourseId
     * @return
     */
    @PostMapping("/selectExchangeDetails")
    private DataResponse selectExchangeDetails(String changeCourseId){
        try {
            ChangeCourseRecordVO list = productDetailsService.selectExchangeDetails(changeCourseId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
        }catch (Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    /**
     * selectViewDetails
     * 产品管理 -- 产品兑换 --兑换明细 -- 查看明细
     * @Author wangpeng
     * @Date 2021/11/11
     * @param changeCourseId	ID
     * @return
     */
    @PostMapping("/selectViewDetails")
    private DataResponse selectViewDetails(String changeCourseId){
        try {
            CustomerChangeCourseRecordVO list = productDetailsService.selectViewDetails(changeCourseId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
        }catch (Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * selectExchangeOrderProductList
     * @Author wangpeng
     * @Date 2021/11/17
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param orderNumber  订单编号
     * @return
     * 产品兑换
     */
    @RequestMapping("/selectExchangeOrderProductList")
    public DataResponse selectExchangeOrderProductList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                 @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                 @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                 @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                 String orderNumber
    ) {
        try{
            HashMap<Object, Object> map = new HashMap<>();
            List<ProductExchangeaApprovalVO> approval = productDetailsService.selectProductExchangeApprovaLByOrderNumber(orderNumber);
            if(approval.size() > 0){
                map.put("code", 1);
                map.put("msg", "该订单正在审批,无法兑换");
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),map);
            }
            DhPageInfo<FinishOrderProductVO> list = productDetailsService.selectExchangeOrderProductList(pageIndex,pageSize,sortName,sortOrder,orderNumber);
            List<FinishOrderProductVO> list1 = productDetailsService.selectExchangeOrderProductByorderNumber(orderNumber);
            if(list1.size()==0){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
            }else {
                map.put("code",1);
                map.put("msg", "因订单查询出不是已回款、或提成已发放、或到款时间不是当月回款的记录，订单不能进行兑换");
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),map);
            }
        }catch(Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    /**
     * selectCustomerProductList
     * @Author wangpeng
     * @Date 2021/11/23
     * @param
     * @return
     * 添加非会员的产品列表
     */
    @RequestMapping("/selectExchangeCustomerProductList")
    public DataResponse selectExchangeCustomerProductList(String productName) {
        try{
            List<CustomerProductVO> list = productDetailsService.selectExchangeCustomerProductList(productName);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
        }catch(Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * selectExchangeCustomerMemberProductList
     * @Author wangpeng
     * @Date 2021/11/23
     * @param
     * @return
     * 添加会员产品列表
     */
    @RequestMapping("/selectExchangeCustomerMemberProductList")
    public DataResponse selectExchangeCustomerMemberProductList(String memberCardName) {
        try{
            List<CustomerMemberProductVO> list = productDetailsService.selectExchangeCustomerMemberProductList(memberCardName);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
        }catch(Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    /**
     * addProductExchangeform
     * @Author wangpeng
     * @Date 2021/11/22
     * @param qo
     * @return
     * 新增到产品兑换
     */
    @RequestMapping("/addProductExchangeform")
    public DataResponse addProductExchangeform(@RequestBody ProductExchangeFormQO qo){
        try{
            productDetailsService.addProductExchangeform(qo);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        }catch(Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * selectProductExchangeFormList
     * @Author wangpeng
     * @Date 2021/11/22
     * @param orderNumber 订单编号
     * @return
     *产品兑换列表
     */
    @RequestMapping("/selectProductExchangeFormList")
    public DataResponse selectProductExchangeFormList(String orderNumber){
        try{
            List<ProductExchangeFormVO> list = productDetailsService.selectProductExchangeFormList(orderNumber);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
        }catch(Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    /**
     * deleteProductExchangeFormBy
     * @Author wangpeng
     * @Date 2021/11/22
     * @param productExchangeId  id
     * @param delFlag  状态
     * @return
     * 产品兑换列表---删除
     */
    @RequestMapping("/deleteProductExchangeFormBy")
    public DataResponse deleteProductExchangeFormBy(String productExchangeId,String delFlag){
        try{
            Map map = productDetailsService.deleteProductExchangeFormByProductExchangeId(productExchangeId,delFlag);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),map);
        }catch(Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    /**
     * addProductExchangeApproval
     * @Author wangpeng
     * @Date 2021/11/22
     * @param qo
     * @return
     * 新增到审批记录表
     */
    @RequestMapping("/addProductExchangeApproval")
    public DataResponse addProductExchangeApproval(@RequestBody ProductExchangeaApprovalQO qo) {
        try{
            Map map = productDetailsService.addProductExchangeApproval(qo);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),map);
        }catch(Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * selectProductExchangeApprovalList
     * @Author wangpeng
     * @Date 2021/11/22
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @return
     * 查看审批列表
     */
    @RequestMapping("/selectProductExchangeApprovalList")
    public DataResponse selectProductExchangeApprovalList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                         @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                         @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                         @RequestParam(value="sortOrder",required = false) String sortOrder//排序规则(asc/desc)
                                                         ) {
        try{
            DhPageInfo<ProductExchangeaApprovalVO> list=productDetailsService.selectProductExchangeApprovalList(pageIndex,pageSize,sortName,sortOrder);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
        }catch(Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * updateProductExchangeApproval
     * @Author wangpeng
     * @Date 2021/11/22
     * @param productExchangeApprovalId	 ID
     * @param approvalStatus	审批状态
     * @param approvalRemarks	审批备注
     * @param exchangeFinishOrderProductId	兑换前产品id
     * @param exchangeNumber    产品兑换编码（记录表生成的编号）
     * @return
     *  客服部 --审批--提交审批
     */
    @RequestMapping("/updateProductExchangeApproval")
    public DataResponse updateProductExchangeApproval(String productExchangeApprovalId,String approvalStatus,String approvalRemarks,String exchangeFinishOrderProductId,String exchangeNumber){
        try{
            productDetailsService.updateProductExchangeApproval(productExchangeApprovalId,approvalStatus,approvalRemarks,exchangeFinishOrderProductId,exchangeNumber);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        }catch(Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * selectProductExchangeApprovalByExchangeNumber
     * @Author wangpeng
     * @Date 2021/11/22
     * @param exchangeNumber
     * @param type
     * @return
     * 审批列表展示兑换产品记录
     */
    @RequestMapping("/selectProductExchangeApprovalByExchangeNumber")
    public DataResponse selectProductExchangeApprovalByExchangeNumber(String exchangeNumber,String type){
        try{
            DhPageInfo<ExchangeProductsRecordVO> list = productDetailsService.selectProductExchangeApprovalByExchangeNumber(exchangeNumber,type);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
        }catch(Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * deleteRroductExchangeForm
     * @Author wangpeng
     * @Date 2021/11/22
     * @param productExchangeId
     * @return
     * 取消删除
     */
    @RequestMapping("/deleteRroductExchangeForm")
    public DataResponse deleteRroductExchangeForm(String productExchangeId){
        try{
            productDetailsService.deleteRroductExchangeForm(productExchangeId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        }catch(Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 根据id查询产品审批最新记录
     * selectApprovedform
     * @Author wangpeng
     * @Date 2021/11/30
     * @param productExchangeApprovalId
     * @return
     */
    @RequestMapping("/selectApprovedform")
    public DataResponse selectApprovedform(String productExchangeApprovalId){
        try{
            ProductExchangeaApprovalVO vo =productDetailsService.selectApprovedform(productExchangeApprovalId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),vo);
        }catch(Exception e){
            e.printStackTrace();
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }




}

